/*
package cn.itsource;

import cn.itsource.doc.CourseDoc;


;
import cn.itsource.repository.CourseElasticsearchRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ESServerApplication2050.class)
public class ESTest {

    //es的工具
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;
    //相当于创建库
    @Test
    public void testCreate(){
        //创建索引
        elasticsearchTemplate.createIndex(CourseDoc.class);
        //做文档映射
        elasticsearchTemplate.putMapping(CourseDoc.class);
    }
    //测试添加数据
    @Test
    public void testAdd() {
        CourseDoc courseDoc = new CourseDoc();

        courseDoc.setName("Java菜鸟版");
        courseDoc.setIntro("Java简单");

        courseElasticsearchRepository.save(courseDoc);
            System.out.println("保存成功");
        }
    @Test
    public void testAdd2(){
        for(int i = 0 ; i < 50 ; i++){
            CourseDoc courseDoc = new CourseDoc();
            courseDoc.setId(i+2l);


            if(i % 2 == 0){
                courseDoc.setName("Java入门");
                courseDoc.setIntro("Java真难");

                courseDoc.setCourseTypeId(1000L);
            }else{
                courseDoc.setName("H5入门");
                courseDoc.setIntro("H5真难");
                courseDoc.setCourseTypeId(2000L);
            }

            courseDoc.setPrice(i*2 +100F);

            courseElasticsearchRepository.save(courseDoc);

            System.out.println("保存ok");
        }

    }
    @Test
    public void testSearch(){
        //构建查询对象
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        //查询条件
        //组合查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //name中包含Java
        boolQueryBuilder.must(QueryBuilders.matchQuery("name","入门"));

        List<QueryBuilder> filter = boolQueryBuilder.filter();
        //课程类型是 1000  : 等值查询 term   -> 过滤 必须为1000
        filter.add(QueryBuilders.termQuery("courseTypeId",1000L));
        //价格是 100 - 500 : 范围 range       -> 过滤
        filter.add(QueryBuilders.rangeQuery("price").gte(100).lte(500));
        //把组合对象放入查询对象
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
        //-----------------------------------------------------------------------------------------------
        //分页查询第1页，每页100条
        nativeSearchQueryBuilder.withPageable(PageRequest.of(0,100));
        //价格   倒排序                                                                   倒排序
        nativeSearchQueryBuilder.withSort(new FieldSortBuilder("price").order(SortOrder.DESC));


        //根据builder构建查询对象
        SearchQuery searchQuery = nativeSearchQueryBuilder.build();

        //执行查询
        Page<CourseDoc> page = courseElasticsearchRepository.search(searchQuery);

        //处理结果=====================================================================================
        //获取结果
        long totalElements = page.getTotalElements();
        System.out.println("条数："+totalElements);
        int totalPages = page.getTotalPages();
        System.out.println("页数："+totalPages);
        page.getContent().forEach(c->{
            System.out.println(c);
        });
    }

    }

*/
